package com.example.choupaiapp.main.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.choupaiapp.R;
import com.example.choupaiapp.common.constant.Contacts;
import com.example.choupaiapp.common.finalokgo.callback.Callback;
import com.example.choupaiapp.common.finalokgo.request.RequestType;
import com.example.choupaiapp.common.ui.activity.CommonActivity;
import com.example.choupaiapp.common.util.ActivityManager;
import com.example.choupaiapp.common.util.SharedPreferencesHelp;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.main.bean.OpenIdIsRegistBean;
import com.example.choupaiapp.main.bean.TokenBean;
import com.example.choupaiapp.main.bean.UserInfoBean;
import com.example.choupaiapp.main.bean.WechatLoginBean;
import com.example.choupaiapp.main.bean.WechatLoginUserInfoBean;
import com.example.choupaiapp.main.callback.WechatCallBack;
import com.example.choupaiapp.main.constant.Config;
import com.example.choupaiapp.main.constant.Url;
import com.example.choupaiapp.main.util.WechatLoginUtil;
import com.example.choupaiapp.main.view.ScollLinearLayoutManager;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.orhanobut.logger.Logger;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.utils.UMUtils;
import com.umeng.umverify.UMVerifyHelper;
import com.umeng.umverify.listener.UMPreLoginResultListener;
import com.umeng.umverify.listener.UMTokenResultListener;
import com.umeng.umverify.model.UMTokenRet;
import com.umeng.umverify.view.UMAuthUIConfig;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by ASUS on 2020/4/20.
 */

public class ChoseLoginActivity extends CommonActivity implements WechatCallBack {

    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;
    private UMVerifyHelper umVerifyHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chose_login);
    }

    @Override
    protected void initView() {

        BaseQuickAdapter<String, BaseViewHolder> quickAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_fit_image) {
            @Override
            protected void convert(BaseViewHolder helper, String item) {
            }

        };

        ScollLinearLayoutManager layoutManager = new ScollLinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler_view.setLayoutManager(layoutManager);
        recycler_view.setAdapter(quickAdapter);

        for (int i = 0; i < 100; i++) {
            quickAdapter.addData("");
        }
        quickAdapter.notifyDataSetChanged();

        recycler_view.smoothScrollToPosition(99);
    }

    @Override
    protected void initData() {
        if (!TextUtils.isEmpty(UMUtils.getAppkey(this))) {
            Logger.e("SDK初始化验证通过");
        }
        UMTokenResultListener umTokenResultListener = new UMTokenResultListener() {
            @Override
            public void onTokenSuccess(String s) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Logger.e("s:" + s);
                        UMTokenRet tokenRet = null;
                        try {
                            tokenRet = JSON.parseObject(s, UMTokenRet.class);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (tokenRet != null && !("600001").equals(tokenRet.getCode())) {
                            String token = tokenRet.getToken();
                            JSONObject jsonObject = new JSONObject();
                            try {
                                jsonObject.put("token", token);
                                jsonObject.put("appkey", Config.UMENG_APP_KEY);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            finalOkGo.request(RequestType.POSTJSON, Url.RIGIST_LOGIN, null, jsonObject.toString(), new Callback<TokenBean>() {
                                @Override
                                public void onSuccess(TokenBean tokenBean) {
                                    if (tokenBean.getCode() == Contacts.SUCCESS) {
                                        SharedPreferencesHelp.put("token", tokenBean.getToken());
                                        finalOkGo.requestHead(RequestType.GETHEAD, Url.GET_UERINFO, null, Url.GET_UERINFO, new Callback<UserInfoBean>() {
                                            @Override
                                            public void onSuccess(UserInfoBean userInfoBean) {
                                                if (userInfoBean.getCode() == Contacts.SUCCESS) {
                                                    SharedPreferencesHelp.putBean("userInfo", userInfoBean);
                                                    CrashReport.setUserId(context, userInfoBean.getUser().getMobile());
                                                    if (tokenBean.getFirst() == 1) {
                                                        EMClient.getInstance().login("HX_" + userInfoBean.getUser().getUserId(), "HX_" + userInfoBean.getUser().getUserId(), new EMCallBack() {//回调
                                                            @Override
                                                            public void onSuccess() {
                                                                umVerifyHelper.hideLoginLoading();
                                                                umVerifyHelper.quitLoginPage();
                                                                dismissDialog();
                                                                EMClient.getInstance().groupManager().loadAllGroups();
                                                                EMClient.getInstance().chatManager().loadAllConversations();
                                                                JPushInterface.setAlias(context, 0, Config.JPUS_PRE + userInfoBean.getUser().getUserId());
                                                                MobclickAgent.onProfileSignIn("CP_" + userInfoBean.getUser().getUserId());
                                                                Bundle bundle = new Bundle();
                                                                bundle.putInt("flag", 1);
                                                                startFinishActivity(ChangePersonalInfoActivity.class, bundle);
                                                            }

                                                            @Override
                                                            public void onProgress(int progress, String status) {

                                                            }

                                                            @Override
                                                            public void onError(int code, final String message) {
                                                                runOnUiThread(() -> {
                                                                    Logger.e(code + ":" + message);
                                                                    umVerifyHelper.hideLoginLoading();
                                                                    umVerifyHelper.quitLoginPage();
                                                                    dismissDialog();
                                                                    JPushInterface.setAlias(context, 0, Config.JPUS_PRE + userInfoBean.getUser().getUserId());
                                                                    MobclickAgent.onProfileSignIn("CP_" + userInfoBean.getUser().getUserId());
                                                                    showImageToas("即时通讯登录失败 私信功能不能使用");
                                                                    Bundle bundle = new Bundle();
                                                                    bundle.putInt("flag", 2);
                                                                    startFinishActivity(ChangePersonalInfoActivity.class, bundle);
                                                                });

                                                            }
                                                        });
                                                    } else {
                                                        EMClient.getInstance().login("HX_" + userInfoBean.getUser().getUserId(), "HX_" + userInfoBean.getUser().getUserId(), new EMCallBack() {//回调
                                                            @Override
                                                            public void onSuccess() {
                                                                umVerifyHelper.hideLoginLoading();
                                                                umVerifyHelper.quitLoginPage();
                                                                dismissDialog();
                                                                EMClient.getInstance().groupManager().loadAllGroups();
                                                                EMClient.getInstance().chatManager().loadAllConversations();
                                                                JPushInterface.setAlias(context, 0, Config.JPUS_PRE + userInfoBean.getUser().getUserId());
                                                                MobclickAgent.onProfileSignIn("CP_" + userInfoBean.getUser().getUserId());
                                                                ActivityManager.getActivityManager().popAllActivity();
                                                                startFinishActivity(MainActivity.class);
                                                            }

                                                            @Override
                                                            public void onProgress(int progress, String status) {

                                                            }

                                                            @Override
                                                            public void onError(int code, final String message) {
                                                                runOnUiThread(() -> {
                                                                    Logger.e(code + ":" + message);
                                                                    umVerifyHelper.hideLoginLoading();
                                                                    umVerifyHelper.quitLoginPage();
                                                                    dismissDialog();
                                                                    JPushInterface.setAlias(context, 0, Config.JPUS_PRE + userInfoBean.getUser().getUserId());
                                                                    MobclickAgent.onProfileSignIn("CP_" + userInfoBean.getUser().getUserId());
                                                                    showImageToas("即时通讯登录失败 私信功能不能使用");
                                                                    ActivityManager.getActivityManager().popAllActivity();
                                                                    startFinishActivity(MainActivity.class);
                                                                });

                                                            }
                                                        });
                                                    }
                                                } else {
                                                    umVerifyHelper.hideLoginLoading();
                                                    umVerifyHelper.quitLoginPage();
                                                    dismissDialog();
                                                    if (userInfoBean.getCode() == Contacts.TOKEN_EMPTY) {
                                                    } else {
                                                        showImageToas(userInfoBean.getMsg());
                                                    }
                                                }
                                            }

                                            @Override
                                            public void onError(Response<String> response) {
                                                super.onError(response);
                                                dismissDialog();
                                                umVerifyHelper.hideLoginLoading();
                                                umVerifyHelper.quitLoginPage();
                                            }
                                        });
                                    } else {
                                        umVerifyHelper.hideLoginLoading();
                                        umVerifyHelper.quitLoginPage();
                                        dismissDialog();
                                        if (tokenBean.getCode() == Contacts.TOKEN_EMPTY) {
                                        } else {
                                            showImageToas(tokenBean.getMsg());
                                        }
                                    }
                                }

                                @Override
                                public void onError(Response<String> response) {
                                    super.onError(response);
                                    dismissDialog();
                                    umVerifyHelper.hideLoginLoading();
                                    umVerifyHelper.quitLoginPage();
                                }
                            });

                        }
                    }
                });

            }

            @Override
            public void onTokenFailed(String s) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        umVerifyHelper.hideLoginLoading();
                        umVerifyHelper.quitLoginPage();
                    }
                });
            }
        };

        umVerifyHelper = UMVerifyHelper.getInstance(this, umTokenResultListener);
        umVerifyHelper.setAuthSDKInfo(Config.UMENG_SCECET_KEY);
        umVerifyHelper.setAuthListener(umTokenResultListener);
        umVerifyHelper.setLoggerEnable(true);
        umVerifyHelper.accelerateLoginPage(5000, new UMPreLoginResultListener() {
            @Override
            public void onTokenSuccess(String s) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Logger.e("预取号成功:" + s);
                    }
                });
            }

            @Override
            public void onTokenFailed(String s, String s1) {

            }
        });
    }

    @OnClick({R.id.wechat_login, R.id.phone_login, R.id.other_login})
    public void onViewClicked(View view) {
        if (Utils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.other_login:
                startActivity(OtherLoginActivity.class);
                break;
            case R.id.phone_login:
//                startActivity(PhoneLoginActivity.class);
                boolean check = umVerifyHelper.checkEnvAvailable();
                if (check) {
                    UMAuthUIConfig umAuthUIConfig = new UMAuthUIConfig.Builder()
                            .setStatusBarHidden(false)
                            .setWebViewStatusBarColor(getResources().getColor(R.color.colorPrimaryDark))
                            .setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark))
                            .setLightColor(false)
                            .setNavHidden(false)
                            .setNavColor(getResources().getColor(R.color.white))
                            .setNavText("本机登录")
                            .setNavTextColor(getResources().getColor(R.color.color_222222))
                            .setNavTextSize(15)
                            .setNavReturnHidden(false)
                            .setSloganHidden(true)
                            .setSwitchAccHidden(true)
                            .setNavReturnImgPath("login_back_b")
                            .setLogoImgPath("login_iphone_icon")
                            .setLogoWidth(100)
                            .setLogoHeight(100)
                            .setLogoOffsetY(110)
                            .setNumberColor(getResources().getColor(R.color.color_333))
                            .setNumberSize(17)
                            .setNumberLayoutGravity(Gravity.CENTER_HORIZONTAL)
                            .setNumFieldOffsetY(244)
                            .setLogBtnText("一键登录")
                            .setLogBtnTextColor(getResources().getColor(R.color.color_333))
                            .setLogBtnTextSize(17)
                            .setLogBtnWidth(215)
                            .setLogBtnHeight(68)
                            .setLogBtnBackgroundPath("login_bg")
                            .setLogBtnOffsetY(282)
                            .setLogBtnLayoutGravity(Gravity.CENTER_HORIZONTAL)
                            .setAppPrivacyOne("丑拍使用协议", "http://app.tisuapp.com/app-web/html/iosAgreement.html")
                            .setAppPrivacyColor(getResources().getColor(R.color.color_333), getResources().getColor(R.color.color_CEB06E))
                            .setPrivacyOffsetY_B(30)
                            .setPrivacyState(true)
                            .setProtocolGravity(Gravity.CENTER_HORIZONTAL)
                            .setPrivacyTextSize(10)
                            .setPrivacyMargin(85)
                            .setPrivacyBefore("登录即同意")
                            .setPrivacyEnd("并使用本机号码登录")
                            .setUncheckedImgPath("xy_btn_normal")
                            .setCheckedImgPath("xy_btn_selected")
                            .setVendorPrivacyPrefix("《")
                            .setVendorPrivacySuffix("》")
                            .setProtocolLayoutGravity(Gravity.CENTER_HORIZONTAL)
                            .create();
                    umVerifyHelper.removeAuthRegisterXmlConfig();
                    umVerifyHelper.removeAuthRegisterViewConfig();
                    umVerifyHelper.setAuthUIConfig(umAuthUIConfig);
                    umVerifyHelper.getLoginToken(this, 5000);
                } else {
                    showImageToas("请打开您的4g网络");
                }
                break;
            case R.id.wechat_login:
                if (WechatLoginUtil.getInstance().isWeChatAppInstalled()) {
                    showDialog(this, "登录中...");
                    if (!TextUtils.isEmpty((String) SharedPreferencesHelp.get("weChat_token", ""))) {
                        finalOkGo.request(RequestType.GET, "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + Config.WEIXIN_APP_ID
                                        + "&grant_type=refresh_token&refresh_token=" + SharedPreferencesHelp.get("weChat_token", ""),
                                null, "refresh_token",
                                new Callback<WechatLoginBean>() {
                                    @Override
                                    public void onSuccess(WechatLoginBean refresh) {
                                        if (refresh != null && refresh.getOpenid() != null && !TextUtils.isEmpty(refresh.getOpenid())) {
                                            SharedPreferencesHelp.put("weChat_token", refresh.getRefresh_token());
                                            HttpParams params = new HttpParams();
                                            params.put("wechatOpenid", refresh.getOpenid());
                                            finalOkGo.request(RequestType.POST, Url.OPEN_ID_IS_RIGIST, params, Url.OPEN_ID_IS_RIGIST, new Callback<OpenIdIsRegistBean>() {
                                                @Override
                                                public void onSuccess(OpenIdIsRegistBean openIdIsRegistBean) {
                                                    if (openIdIsRegistBean.getCode() == Contacts.SUCCESS) {
                                                        if (openIdIsRegistBean.getVerify() == 0) {
                                                            finalOkGo.request(RequestType.GET, "https://api.weixin.qq.com/sns/userinfo?access_token="
                                                                            + refresh.getAccess_token()
                                                                            + "&openid=" + refresh.getOpenid(),
                                                                    null, "wechatInfo", new Callback<WechatLoginUserInfoBean>() {
                                                                        @Override
                                                                        public void onSuccess(WechatLoginUserInfoBean wechatLoginUserInfoBean) {
                                                                            if (wechatLoginUserInfoBean.getErrcode() != 40003) {
                                                                                dismissDialog();
                                                                                Bundle bundle = new Bundle();
                                                                                bundle.putString("headUrl", wechatLoginUserInfoBean.getHeadimgurl());
                                                                                bundle.putString("name", wechatLoginUserInfoBean.getNickname());
                                                                                bundle.putString("sex", wechatLoginUserInfoBean.getSex() + "");
                                                                                bundle.putString("openId", wechatLoginUserInfoBean.getOpenid());
                                                                                bundle.putString("flag", "wechat");
                                                                                startActivity(BindPhoneActivity.class, bundle);
                                                                            } else {
                                                                                dismissDialog();
                                                                                showImageToas("获取用户信息失败");
                                                                            }
                                                                        }

                                                                        @Override
                                                                        public void onError(Response<String> response) {
                                                                            super.onError(response);
                                                                            dismissDialog();
                                                                        }
                                                                    });

                                                        } else {
                                                            SharedPreferencesHelp.put("token", openIdIsRegistBean.getToken());
                                                            finalOkGo.requestHead(RequestType.GETHEAD, Url.GET_UERINFO, null, Url.GET_UERINFO, new Callback<UserInfoBean>() {
                                                                @Override
                                                                public void onSuccess(UserInfoBean userInfoBean) {
                                                                    if (userInfoBean.getCode() == Contacts.SUCCESS) {
                                                                        SharedPreferencesHelp.putBean("userInfo", userInfoBean);
                                                                        CrashReport.setUserId(context, userInfoBean.getUser().getMobile());
                                                                        EMClient.getInstance().login("HX_" + userInfoBean.getUser().getUserId(), "HX_" + userInfoBean.getUser().getUserId(), new EMCallBack() {//回调
                                                                            @Override
                                                                            public void onSuccess() {
                                                                                dismissDialog();
                                                                                EMClient.getInstance().groupManager().loadAllGroups();
                                                                                EMClient.getInstance().chatManager().loadAllConversations();
                                                                                JPushInterface.setAlias(context, 0, Config.JPUS_PRE + userInfoBean.getUser().getUserId());
                                                                                MobclickAgent.onProfileSignIn("Wechat", "CP_" + userInfoBean.getUser().getUserId());
                                                                                ActivityManager.getActivityManager().popAllActivity();
                                                                                startFinishActivity(MainActivity.class);
                                                                            }

                                                                            @Override
                                                                            public void onProgress(int progress, String status) {

                                                                            }

                                                                            @Override
                                                                            public void onError(int code, final String message) {
                                                                                runOnUiThread(() -> {
                                                                                    dismissDialog();
                                                                                    JPushInterface.setAlias(context, 0, Config.JPUS_PRE + userInfoBean.getUser().getUserId());
                                                                                    MobclickAgent.onProfileSignIn("Wechat", "CP_" + userInfoBean.getUser().getUserId());
                                                                                    showImageToas("即时通讯登录失败 私信功能不能使用");
                                                                                    ActivityManager.getActivityManager().popAllActivity();
                                                                                    startFinishActivity(MainActivity.class);
                                                                                });

                                                                            }
                                                                        });
                                                                    } else {
                                                                        dismissDialog();
                                                                        showImageToas(userInfoBean.getMsg());
                                                                    }
                                                                }

                                                                @Override
                                                                public void onError(Response<String> response) {
                                                                    super.onError(response);
                                                                    dismissDialog();
                                                                }
                                                            });
                                                        }
                                                    } else {
                                                        dismissDialog();
                                                        showImageToas(openIdIsRegistBean.getMsg());
                                                    }
                                                }

                                                @Override
                                                public void onError(Response<String> response) {
                                                    super.onError(response);
                                                    dismissDialog();
                                                }
                                            });
                                        } else {
                                            WechatLoginUtil.getInstance().setWechatCallBack(ChoseLoginActivity.this);
                                            WechatLoginUtil.getInstance().wechatLogin();
                                        }
                                    }

                                    @Override
                                    public void onError(Response<String> response) {
                                        super.onError(response);
                                        dismissDialog();
                                    }
                                });
                    } else {
                        WechatLoginUtil.getInstance().setWechatCallBack(this);
                        WechatLoginUtil.getInstance().wechatLogin();
                    }
                } else {
                    showImageToas("请先安装微信客户端");
                }
                break;
        }
    }

    @Override
    public void callBack(BaseResp baseResp) {
        String result = "";
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                Logger.e("sssssssssss");
                SendAuth.Resp newResp = (SendAuth.Resp) baseResp;
                finalOkGo.request(RequestType.GET,
                        "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + Config.WEIXIN_APP_ID
                                + "&secret=" + Config.WEIXIN_APP_SCRECT + "&code=" + newResp.code
                                + "&grant_type=authorization_code", null, "wechatLogin", new Callback<WechatLoginBean>() {
                            @Override
                            public void onSuccess(WechatLoginBean wechatLoginBean) {
                                if (wechatLoginBean != null && wechatLoginBean.getOpenid() != null && !TextUtils.isEmpty(wechatLoginBean.getOpenid())) {
                                    finalOkGo.request(RequestType.GET, "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + Config.WEIXIN_APP_ID
                                                    + "&grant_type=refresh_token&refresh_token=" + wechatLoginBean.getRefresh_token(), null, "refresh_token",
                                            new Callback<WechatLoginBean>() {
                                                @Override
                                                public void onSuccess(WechatLoginBean refresh) {
                                                    if (refresh != null && refresh.getOpenid() != null && !TextUtils.isEmpty(refresh.getOpenid())) {
                                                        SharedPreferencesHelp.put("weChat_token", refresh.getRefresh_token());
                                                        HttpParams params = new HttpParams();
                                                        params.put("wechatOpenid", refresh.getOpenid());
                                                        finalOkGo.request(RequestType.POST, Url.OPEN_ID_IS_RIGIST, params, Url.OPEN_ID_IS_RIGIST, new Callback<OpenIdIsRegistBean>() {
                                                            @Override
                                                            public void onSuccess(OpenIdIsRegistBean openIdIsRegistBean) {
                                                                if (openIdIsRegistBean.getCode() == Contacts.SUCCESS) {
                                                                    if (openIdIsRegistBean.getVerify() == 0) {
                                                                        finalOkGo.request(RequestType.GET, "https://api.weixin.qq.com/sns/userinfo?access_token="
                                                                                        + refresh.getAccess_token()
                                                                                        + "&openid=" + refresh.getOpenid(),
                                                                                null, "wechatInfo", new Callback<WechatLoginUserInfoBean>() {
                                                                                    @Override
                                                                                    public void onSuccess(WechatLoginUserInfoBean wechatLoginUserInfoBean) {
                                                                                        if (wechatLoginUserInfoBean.getErrcode() != 40003) {
                                                                                            dismissDialog();
                                                                                            Bundle bundle = new Bundle();
                                                                                            bundle.putString("headUrl", wechatLoginUserInfoBean.getHeadimgurl());
                                                                                            bundle.putString("name", wechatLoginUserInfoBean.getNickname());
                                                                                            bundle.putString("sex", wechatLoginUserInfoBean.getSex() + "");
                                                                                            bundle.putString("openId", wechatLoginUserInfoBean.getOpenid());
                                                                                            bundle.putString("flag", "wechat");
                                                                                            startActivity(BindPhoneActivity.class, bundle);
                                                                                        } else {
                                                                                            dismissDialog();
                                                                                            showImageToas("获取用户信息失败");
                                                                                        }
                                                                                    }

                                                                                    @Override
                                                                                    public void onError(Response<String> response) {
                                                                                        super.onError(response);
                                                                                        dismissDialog();
                                                                                    }
                                                                                });

                                                                    } else {
                                                                        SharedPreferencesHelp.put("token", openIdIsRegistBean.getToken());
                                                                        finalOkGo.requestHead(RequestType.GETHEAD, Url.GET_UERINFO, null, Url.GET_UERINFO, new Callback<UserInfoBean>() {
                                                                            @Override
                                                                            public void onSuccess(UserInfoBean userInfoBean) {
                                                                                if (userInfoBean.getCode() == Contacts.SUCCESS) {
                                                                                    SharedPreferencesHelp.putBean("userInfo", userInfoBean);
                                                                                    CrashReport.setUserId(context, userInfoBean.getUser().getMobile());
                                                                                    EMClient.getInstance().login("HX_" + userInfoBean.getUser().getUserId(), "HX_" + userInfoBean.getUser().getUserId(), new EMCallBack() {//回调
                                                                                        @Override
                                                                                        public void onSuccess() {
                                                                                            dismissDialog();
                                                                                            EMClient.getInstance().groupManager().loadAllGroups();
                                                                                            EMClient.getInstance().chatManager().loadAllConversations();
                                                                                            JPushInterface.setAlias(context, 0, Config.JPUS_PRE + userInfoBean.getUser().getUserId());
                                                                                            MobclickAgent.onProfileSignIn("Wechat", "CP_" + userInfoBean.getUser().getUserId());
                                                                                            ActivityManager.getActivityManager().popAllActivity();
                                                                                            startFinishActivity(MainActivity.class);
                                                                                        }

                                                                                        @Override
                                                                                        public void onProgress(int progress, String status) {

                                                                                        }

                                                                                        @Override
                                                                                        public void onError(int code, final String message) {
                                                                                            runOnUiThread(() -> {
                                                                                                dismissDialog();
                                                                                                JPushInterface.setAlias(context, 0, Config.JPUS_PRE + userInfoBean.getUser().getUserId());
                                                                                                MobclickAgent.onProfileSignIn("Wechat", "CP_" + userInfoBean.getUser().getUserId());
                                                                                                showImageToas("即时通讯登录失败 私信功能不能使用");
                                                                                                ActivityManager.getActivityManager().popAllActivity();
                                                                                                startFinishActivity(MainActivity.class);
                                                                                            });

                                                                                        }
                                                                                    });
                                                                                } else {
                                                                                    dismissDialog();
                                                                                    showImageToas(userInfoBean.getMsg());
                                                                                }
                                                                            }

                                                                            @Override
                                                                            public void onError(Response<String> response) {
                                                                                super.onError(response);
                                                                                dismissDialog();
                                                                            }
                                                                        });
                                                                    }
                                                                } else {
                                                                    dismissDialog();
                                                                    showImageToas(openIdIsRegistBean.getMsg());
                                                                }
                                                            }

                                                            @Override
                                                            public void onError(Response<String> response) {
                                                                super.onError(response);
                                                                dismissDialog();
                                                            }
                                                        });
                                                    } else {
                                                        dismissDialog();
                                                        showImageToas("获取用户信息失败");
                                                    }
                                                }

                                                @Override
                                                public void onError(Response<String> response) {
                                                    super.onError(response);
                                                    dismissDialog();
                                                }
                                            });

                                } else {
                                    dismissDialog();
                                    showImageToas("获取用户信息失败");
                                }
                            }

                            @Override
                            public void onError(Response<String> response) {
                                super.onError(response);
                                dismissDialog();
                            }
                        });
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                dismissDialog();
                result = "发送取消";
                showImageToas(result);
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                dismissDialog();
                result = "发送被拒绝";
                showImageToas(result);
                break;
            default:
                dismissDialog();
                result = "发送返回";
                showImageToas(result);
                break;
        }
    }
}
